﻿using CodeNotes.BLL;
using CodeNotes.Common;
using CodeNotes.Model;
using MvcCodeNotes.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;

namespace MvcCodeNotes.Controllers
{
    public class BookApiController : Controller
    {
        //
        // GET: /BookApi/

        public ActionResult Index()
        {
            ////从首页开始抓
            //List<CW_FetchUrl> list = new CW_FetchUrlLogic().GetModelList("Id<10");
            //foreach(var item in list)
            //{
            //    getHomepage(item.Url);
            //}

            //CW_Book book=new CW_BookLogic().GetModel(87);
            //getChapterList_8ds(book);
            return View();
        }
        #region 88读书 http://www.8dushu.com/


        [HttpPost]
        public JsonResult GetSortList_88(int sid, int lid)
        {
            string url = "http://www.8dushu.com/sort" + sid;
            for (int p = 1; p <= lid; p++)
            {
                try
                {
                    string fetchUrl = url + "/" + p + "/";
                    string html = BookFetch.GetHtml(fetchUrl, "GBK");
                    string pattern = "<a href=\"(?<bookurl>/xiaoshuo/\\d+/\\d+/)\"><b>(?<bookname>[^<]*)</b></a>";
                    MatchCollection mContent = Regex.Matches(html, pattern, RegexOptions.IgnoreCase);
                    if (mContent.Count > 0)
                    {
                        for (int i = 0; i < mContent.Count; i++)
                        {
                            string bookurl = mContent[i].Groups["bookurl"].Value.Trim();
                            string bookname = mContent[i].Groups["bookname"].Value.Trim();

                            if (bookurl != "" && bookname != "")
                            {
                                string urlDetail = "http://www.8dushu.com" + bookurl;
                                //判断这个小说有没有被抓取过
                                int existCount = new CW_BookLogic().GetRecordCount("Source='" + urlDetail + "'");
                                if (existCount == 0)
                                {
                                    CW_Book book = new CW_Book();
                                    book.BookName = bookname;
                                    book.Source = urlDetail;
                                    book.StarCount = 0;
                                    book.ClickCount = 0;
                                    book.IsOver = false;
                                    book.CreateDate = DateTime.Now;
                                    book.Category = sid;
                                    book.IsShow = false;
                                    new CW_BookLogic().Add(book);
                                }

                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    CW_BookFetchLog log = new CW_BookFetchLog();
                    log.BookName = "根据类型抓取小说并保存到book表出错";
                    log.FetchUrl = url;
                    log.ErrorMsg = ex.Message.ToString();
                    log.CreateDate = DateTime.Now;
                    new CW_BookFetchLogLogic().Add(log);
                }
            }

            var obj = new
            {
                state = "success"
            };

            return Json(obj);
        }

        public JsonResult fetchChapterList_88()
        {
            List<CW_Book> bookList = new CW_BookLogic().GetModelList("IsShow=0");
            foreach(var book in bookList)
            {
                getChapterList_8ds(book);
            }
            var obj = new
            {
                state = "success"
            };

            return Json(obj);
        }

        public void getHomepage(string url)
        {
            bool isError = false;
            string errorMsg = "";
            try
            {
                string siteHost = "http://www.8dushu.com";
                string html = BookFetch.GetHtml(url, "GBK");
                Regex regReplace = new Regex("<script[\\s\\S]*?>[\\s\\S]*?</script>", RegexOptions.Multiline | RegexOptions.IgnoreCase);
                html = regReplace.Replace(html, "").Replace("&lt;div id=&quot;ad_250_left&quot;&gt;&lt;/div&gt;", "");

                string pattern = "<a\\s+href=\"(http://www.8dushu.com)?(?<sorturl>/sort\\d+/\\d+/)\">(?<sortname>[^<]+)</a>";
                MatchCollection mContent = Regex.Matches(html, pattern, RegexOptions.IgnoreCase);
                if (mContent.Count > 0)
                {
                    for (int i = 0; i < mContent.Count; i++)
                    {
                        string sorturl = mContent[i].Groups["sorturl"].Value.Trim();
                        string sortname = mContent[i].Groups["sortname"].Value.Trim();
                        if (sorturl != "")
                        {
                            //判断此网址是否已保存
                            string fetchUrl = siteHost + sorturl;
                            int existCount = new CW_FetchUrlLogic().GetRecordCount("Url='" + fetchUrl + "'");
                            if (existCount == 0)
                            {
                                CW_FetchUrl fetch = new CW_FetchUrl();
                                fetch.SiteName = siteHost;
                                fetch.BookName = sortname;
                                fetch.Url = fetchUrl;
                                fetch.Encoding = "GBK";
                                fetch.IsBookOver = false;
                                fetch.IsFetch = false;
                                fetch.CreateTime = DateTime.Now;
                                new CW_FetchUrlLogic().Add(fetch);
                                getHomepage(fetchUrl);
                            }
                        }
                    }
                }
                else
                {
                    isError = true;
                    errorMsg = "正则没匹配到数据";
                }
            }
            catch (Exception ex)
            {
                isError = true;
                errorMsg = ex.Message.ToString();
            }
            if (isError)
            {
                CW_BookFetchLog log = new CW_BookFetchLog();
                log.BookName = "";
                log.FetchUrl = url;
                log.ErrorMsg = errorMsg;
                log.CreateDate = DateTime.Now;
                new CW_BookFetchLogLogic().Add(log);
            }

        }



        //小说目录_8dushu
        public void getChapterList_8ds(CW_Book book)
        {            
            string EncodingStr = "GBK";

            bool isError = false;
            string errorMsg = "";
            try
            {
                string html = BookFetch.GetHtml(book.Source, EncodingStr);           
                if (string.IsNullOrEmpty(html))
                {
                    isError = true;
                    errorMsg = "小说目录页面，此页面抓取为空;bookid=" + book.Id;
                }
                else
                {
                    string patternAuthor = "<meta property=\"og:novel:author\" content=\"(?<author>[^<]*)\">";
                    MatchCollection mAuthor = Regex.Matches(html, patternAuthor, RegexOptions.IgnoreCase);
                    if (mAuthor.Count > 0)
                    {
                        string author = mAuthor[0].Groups["author"].Value.Trim();
                        book.Author = author;
                    }
                    string patternDesc = "<meta property=\"og:description\"\\s+content=\"(?<desc>[^<]*)\">";
                    MatchCollection mDesc = Regex.Matches(html, patternDesc, RegexOptions.IgnoreCase);
                    if (mDesc.Count > 0)
                    {
                        string desc = mDesc[0].Groups["desc"].Value.Trim();
                        book.Descriotion = desc;
                    }
                    
                    string pattern = "<li><a\\s+href=\"(?<id>\\d+).html\">(?<title>[\\s\\S]*?)</a></li>";
                    MatchCollection mContent = Regex.Matches(html, pattern, RegexOptions.IgnoreCase);
                    if (mContent.Count > 0)
                    {
                        bool isSucc=true;
                        for (int i = 0; i < mContent.Count; i++)
                        {
                            string id = mContent[i].Groups["id"].Value.Trim();
                            string title = mContent[i].Groups["title"].Value.Trim();

                            if (id != "")
                            {
                                string urlDetail = book.Source + id + ".html";
                                //判断这个章节有没有被抓取过
                                int existCount = new CW_BookChapterLogic().GetRecordCount("Source='" + urlDetail + "' and BookId=" + book.Id);
                                if (existCount == 0)
                                {
                                    isSucc=isSucc&&getChapter_8ds(id, title, urlDetail, book.BookName, book.Id, EncodingStr);
                                }
                                    
                            }
                        }

                        if(isSucc)
                        {
                            book.IsShow=true;
                        }
                        book.UpdateDate=DateTime.Now;
                        new CW_BookLogic().Update(book);
                    }
                    else
                    {
                        isError = true;
                        errorMsg = "小说目录页面--抓取章节地址，【正则表达式匹配为0条数据】";
                    }
                }
            }
            catch (Exception ex)
            {
                isError = true;
                errorMsg = ex.Message.ToString();
            }
            if (isError)
            {
                CW_BookFetchLog log = new CW_BookFetchLog();
                log.BookName = book.BookName;
                log.FetchUrl = book.Source;
                log.ErrorMsg = errorMsg;
                log.CreateDate = DateTime.Now;
                new CW_BookFetchLogLogic().Add(log);
            }
        }


        //小说单章内容_8dushu
        public bool getChapter_8ds(string id, string chapterTitle, string url, string bookname, int bookid, string EncodingStr)
        {
            bool isSucc=true;
            try
            {
                string html = BookFetch.GetHtml(url, EncodingStr);
                Regex regReplace = new Regex("<script[\\s\\S]*?>[\\s\\S]*?</script>", RegexOptions.Multiline | RegexOptions.IgnoreCase);

                ////去掉小说内容中的a标签,正则表达式错误
                Regex aReplace = new Regex("<a[^<]*>[^<]*</a>", RegexOptions.Multiline | RegexOptions.IgnoreCase);
                html = aReplace.Replace(html, "");


                string patternContent = "<div\\s+class=\"yd_text2\">\\s*(?<content>[\\s\\S]*?)</div>\\s*<div\\s+class=\"yd_ad1\">";
                string m_content = "";
                Regex reg2 = new Regex(patternContent, RegexOptions.Multiline | RegexOptions.IgnoreCase);
                Match mc2 = reg2.Match(html);
                if (mc2.Success)
                    m_content = mc2.Groups["content"].Value.Trim();
                                
                string content = m_content;
                string retTitle = BookFetch.ParseTags(chapterTitle);
                string retContent = content;

                if (!string.IsNullOrEmpty(retTitle) && !string.IsNullOrEmpty(retContent))
                {
                    CW_BookChapter chapter = new CW_BookChapter()
                    {
                        BookId = bookid,
                        ChapterName = retTitle,
                        ChapterContent = retContent,
                        ClickCount = 0,
                        Source = url,
                        CreateDate = DateTime.Now,
                        Sort = 0
                    };
                    new CW_BookChapterLogic().Add(chapter);
                }

            }
            catch (Exception ex)
            {
                isSucc=false;
                CW_BookFetchLog log = new CW_BookFetchLog();
                log.BookName = bookname + "——" + chapterTitle;
                log.FetchUrl = url;
                log.ErrorMsg = ex.Message.ToString();
                log.CreateDate = DateTime.Now;
                new CW_BookFetchLogLogic().Add(log);
            }
            return isSucc;

        }
        #endregion

        #region 棉花糖
        //小说目录_棉花糖
        public void getChapterList()
        {
            //http://www.8dushu.com/xiaoshuo/64/64994/   养鬼为夫
            //http://book.zongheng.com/showchapter/373142.html
            //棉花糖网站
            string url = "http://www.mianhuatang.la/0/707/";
            string bookname = "兽武乾坤";
            string EncodingStr = "GBK";

            bool isError = false;
            string errorMsg = "";
            try
            {
                CW_Book book = new CW_Book();
                book.BookName = bookname;
                book.ClickCount = 0;
                book.IsOver = false;
                book.Source = url;
                book.CreateDate = DateTime.Now;
                book.UpdateDate = DateTime.Now;
                book.Sort = 0;
                book.IsShow = true;
                int bookid = new CW_BookLogic().Add(book);


                string html = BookFetch.GetHtml(url, EncodingStr);
                Regex regReplace = new Regex("<script[\\s\\S]*?>[\\s\\S]*?</script>", RegexOptions.Multiline | RegexOptions.IgnoreCase);
                html = regReplace.Replace(html, "");

                //去掉最新章节部分
                int idx = html.IndexOf("class=\"novel_list\"");
                if (idx > -1)
                {
                    html = html.Substring(idx);
                }
                if (string.IsNullOrEmpty(html) || bookid <= 0)
                {
                    isError = true;
                    errorMsg = "小说目录页面，此页面抓取为空;bookid=" + bookid;
                }
                else
                {
                    string pattern = "<dd><a\\s+href=\"(?<id>\\d+).html\">(?<title>[\\s\\S]*?)</a></dd>";
                    MatchCollection mContent = Regex.Matches(html, pattern, RegexOptions.IgnoreCase);
                    if (mContent.Count > 0)
                    {
                        for (int i = 0; i < mContent.Count; i++)
                        {
                            string id = mContent[i].Groups["id"].Value.Trim();
                            string title = mContent[i].Groups["title"].Value.Trim();

                            if (id != "")
                            {
                                string urlDetail = url + id + ".html";
                                //判断这个章节有没有被抓取过
                                int existCount = new CW_BookChapterLogic().GetRecordCount("Source='" + urlDetail + "' and BookId=" + bookid);
                                if (existCount == 0)
                                    getChapter(id, title, urlDetail, bookname, bookid, EncodingStr);
                            }
                        }
                    }
                    else
                    {
                        isError = true;
                        errorMsg = "小说目录页面--抓取章节地址，【正则表达式匹配为0条数据】";
                    }
                }
            }
            catch (Exception ex)
            {
                isError = true;
                errorMsg = ex.Message.ToString();
            }
            if (isError)
            {
                CW_BookFetchLog log = new CW_BookFetchLog();
                log.BookName = bookname;
                log.FetchUrl = url;
                log.ErrorMsg = errorMsg;
                log.CreateDate = DateTime.Now;
                new CW_BookFetchLogLogic().Add(log);
            }


        }


        //单章小说_棉花糖
        public void getChapter(string id, string chapterTitle, string url, string bookname, int bookid, string EncodingStr)
        {
            try
            {
                string html = BookFetch.GetHtml(url, EncodingStr);
                Regex regReplace = new Regex("<script[\\s\\S]*?>[\\s\\S]*?</script>", RegexOptions.Multiline | RegexOptions.IgnoreCase);
                html = regReplace.Replace(html, "").Replace(" 一秒记住【棉花糖小说网www.mianhuatang.la】，为您提供精彩小说阅读。", "");

                ////去掉小说内容中的a标签,正则表达式错误
                Regex aReplace = new Regex("<a[^<]*>[^<]*</a>", RegexOptions.Multiline | RegexOptions.IgnoreCase);
                html = aReplace.Replace(html, "");

                string patternContent = "<div\\s+class=\"content\"\\s+id=\"con" + id + "\">\\s*(?<content>[\\s\\S]*?)<div\\s+class=\"con_l\">";
                string m_content = "";
                Regex reg2 = new Regex(patternContent, RegexOptions.Multiline | RegexOptions.IgnoreCase);
                Match mc2 = reg2.Match(html);
                if (mc2.Success)
                    m_content = mc2.Groups["content"].Value.Trim();

                //如果保存成txt格式，得去掉html标签
                //string content = m_content.Replace("<br>", "\r\n").Replace("<br/>", "\r\n").Replace("<br />", "\r\n").Replace("&nbsp;", " ");
                string content = m_content;
                string retTitle = BookFetch.ParseTags(chapterTitle);
                string retContent = content;

                if (!string.IsNullOrEmpty(retTitle) && !string.IsNullOrEmpty(retContent))
                {
                    CW_BookChapter chapter = new CW_BookChapter()
                    {
                        BookId = bookid,
                        ChapterName = retTitle,
                        ChapterContent = retContent,
                        ClickCount = 0,
                        Source = url,
                        CreateDate = DateTime.Now,
                        Sort = 0
                    };
                    new CW_BookChapterLogic().Add(chapter);
                }

            }
            catch (Exception ex)
            {
                CW_BookFetchLog log = new CW_BookFetchLog();
                log.BookName = bookname + "——" + chapterTitle;
                log.FetchUrl = url;
                log.ErrorMsg = ex.Message.ToString();
                log.CreateDate = DateTime.Now;
                new CW_BookFetchLogLogic().Add(log);
            }

        }

        #endregion


        #region 获取本地小说api

        [HttpPost]
        public JsonResult BooksByCategory(int? typeid, int? idx)
        {

            string strWhere = "IsShow=1 ";
            if (typeid != null || typeid > 0)
            {
                strWhere += " and Category=" + typeid;
            }
            if (idx == null || idx < 1)
                idx = 1;
            int index = idx <= 1 ? 1 : Convert.ToInt32(idx);
            SP_Pagination sp = new SP_Pagination
            {
                TableName = "CW_Book",
                Fields = "*",
                OrderField = "Sort",
                PageSize = 20,
                PageIndex = index,
                SortAsc = false,
                strWhere = strWhere
            };
            DataTable dt = sp.GetList().Tables[0];
            List<object> books = new List<object>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {

                string IsOver = dt.Rows[i]["IsOver"].ToString() == "True" ? "已完结" : "连载中";
                int CategoryId = Convert.ToInt32(dt.Rows[i]["Category"].ToString());

                string typeName = Enum.GetName(typeof(EnumBookCategory), CategoryId);
                var book = new
                {
                    id = dt.Rows[i]["Id"].ToString(),
                    bookname = dt.Rows[i]["BookName"].ToString(),
                    author = dt.Rows[i]["Author"].ToString(),
                    desc = dt.Rows[i]["Descriotion"].ToString(),
                    updateDate = Convert.ToDateTime(dt.Rows[i]["UpdateDate"]).ToString("yyyy-MM-dd"),
                    typeName = typeName,
                    isOver = IsOver
                };
                books.Add(book);
            }

            var obj = new
            {
                state = "success",
                books = books
            };
            return Json(obj);
        }

        [HttpPost]
        public JsonResult ChapterList(int? bid)
        {
            string state = "error";
            CW_Book book = new CW_Book();
            List<CW_BookChapter> chapters = new List<CW_BookChapter>();
            if (bid != null && bid > 0)
            {
                book = new CW_BookLogic().GetModel(Convert.ToInt32(bid));
                if (book != null)
                {
                    state = "success";
                    string strFields = " Id,BookId,ChapterName,ClickCount,CreateDate,Sort";
                    string strWhere = "BookId=" + bid + " order by Sort,Id";
                    chapters = new CW_BookChapterLogic().GetFixedColumnModelList(strFields, strWhere);
                }
            }

            var obj = new
            {
                state = state,
                book = book,
                chapters = chapters
            };
            return Json(obj);
        }

        [HttpPost]
        public JsonResult Chapter(int? bid, int? cid)
        {
            string state = "error";
            CW_BookChapter chapter = new CW_BookChapter();
            int nextid = 0;
            if (cid != null && cid > 0)
            {
                chapter = new CW_BookChapterLogic().GetModel(Convert.ToInt32(cid));
                if (chapter != null)
                {
                    state = "success";
                    nextid = getNextChapterid(chapter.BookId, chapter.Id);
                }
            }

            var obj = new
            {
                state = state,
                chapter = chapter,
                nextid = nextid
            };
            return Json(obj);
        }

        public static int getNextChapterid(int bid, int cid)
        {
            int nextid = 0;
            string strFields = " Id,ChapterName,Sort";
            string strWhere = "BookId=" + bid + " order by Sort,Id";
            List<CW_BookChapter> chapters = new CW_BookChapterLogic().GetFixedColumnModelList(strFields, strWhere);
            int idx = chapters.FindIndex(p => p.Id == cid);
            if (chapters.Count > idx + 1)
            {
                nextid = chapters[idx + 1].Id;
            }

            return nextid;
        }

        #endregion

    }
}
